var url = "/openx/logistics/logisticsShield/queryShieldForPage";
var queryForm = 'queryForm';

var ownOptions={};
/**查询参数信息*/
//对应接口入参
var param = {};

var formId = 'queryForm';

//匹配门店编码的正则表达式

var storeCodeRegex = /^([a-zA-Z0-9]{4})(,[a-zA-Z0-9]{4})*$/;

(function () {
//        /**设置增删改查的工具条*/
    $.openx.setTableToolBar('partnerbar');
    /**进行表格数据初始化*/
    var data = $.openx.getJsonObj(formId);
    param.bean = data;
    $.openx.initTable("table", url, param);
    initAreaSelect("province", "city", "district");
    initChannelAndShield("topChannelCode","deliveryType","shieldType");
})();

function initChannelAndShield(first,second,third){
    $X("/logistics/logisticsShield", "queryAllChannel").callx({
        onResult: function (data) {
            if (data) {
                bindSelectWithData(first,data);
            } else {
                layer.msg("无信息")
            }
        },
        onError: function (error) {
            var res = JSON.parse(error);
            layer.msg(res.message)
        }
    });
    $X("/logistics/logisticsShield", "queryAllShieldType").callx({
        onResult: function (data) {
            if (data) {
                bindSelectWithData(second,data);
            } else {
                layer.msg("无信息")
            }
        },
        onError: function (error) {
            var res = JSON.parse(error);
            layer.msg(res.message)
        }
    });
    var data = [{"code":"AREA","name":"按区域"},{"code":"STORE","name":"按门店"}];
    
    bindSelectWithData(third,data);
    $("#add_shield_type").on("change",function(){
    	if($(this).val() === 'AREA'){
    		$(".div-area").show();
    		$(".div-store-codes").hide();
    	}else{
    		$(".div-area").hide();
    		$(".div-store-codes").show();
    	}
    });
    
}



$.openx.insert = function () {

    var createForm = "addShieldForm";
    var innerHtml = $.openx.innerHtml('addShieldDialog');
    laytpl(innerHtml).render({}, function (html) {
        $.openx.openLayer(['auto', '400'], '新增用户物流类型屏蔽管理', html, false, function () {
        	var param = $.openx.getJsonObj(createForm);
        	if ($.openx.validation(createForm)) {
                //获取表单数据
                var topChannelName = $("#add_channel option:selected").text();
                if ($("#add_channel option:selected").val().trim().length === 0) {
                    layer.tips('请选择渠道', '#add_channel_tips', {
                      tips: [2, '#ff4400']
                    });
                    return false;
                  }
               
                
                param.topChannelName = topChannelName;
                if($("#add_shield_type").val() === 'AREA'){
	                var provinceName = $("#add_province option:selected").text();
	                if ($("#add_province option:selected").val().trim().length === 0) {
	                    layer.tips('所属城市不能为空', '#add_province_tips', {
	                      tips: [2, '#ff4400']
	                    });
	                    return false;
	                  }
	                param.provinceName = provinceName;
	                var cityName = $("#add_city option:selected").text();
	                param.cityName = cityName;
                }else{
                	if ($("#storeCodes").val().trim().length === 0) {
                		layer.tips('门店编码不能为空', '#add_store_codes_tips', {
  	                      tips: [2, '#ff4400']
  	                    });
  	                    return false;
                	}
                	if (!storeCodeRegex.test($("#storeCodes").val())){
                         layer.tips('门店编码格式不正确', '#add_store_codes_tips', {
                           tips: [2, '#ff4400']
                         });
                         return false;
                     }
                	param.provinceCode='';
                	param.cityCode='';
                	param.provinceName='';
                	param.cityName='';
                }
                if ($("#add_deliveryType option:selected").val().trim().length === 0) {
                    layer.tips('请选择屏蔽类型', '#add_deliveryType_tips', {
                      tips: [2, '#ff4400']
                    });
                    return false;
                }
                console.log(param);
                
                $X("/logistics/logisticsShield", "createShield").callx({
                    param: param,
                    onResult: function (data) {
                        if (data) {
                            $.openx.tableRefresh(null, null, true);
                            layer.closeAll();
                            layer.msg("新增成功");
                        }
                    },
                    onError: function (error) {
                        var res = JSON.parse(error);
                        layer.msg(res.message)
                    }
                });
            }
        });
    });
    initAreaSelect("add_province", "add_city", "district");
    initChannelAndShield("add_channel","add_deliveryType","add_shield_type");

}



$.openx.update = function () {
    $.openx.onClickUpdate(function (row) {
    	
        var updateForm = "addShieldForm";
        var innerHtml = $.openx.innerHtml('addShieldDialog');
        laytpl(innerHtml).render(row[0], function (html) {
            $.openx.openLayer(['auto', '400'], '修改屏蔽管理信息', html, false, function () {
            	var param = $.openx.getJsonObj(updateForm);
            	if ($.openx.validation(updateForm)) {
                    //获取表单数据
                    var topChannelName = $("#add_channel option:selected").text();
                    if ($("#add_channel option:selected").val().trim().length === 0) {
                        layer.tips('请选择渠道', '#add_channel_tips', {
                          tips: [2, '#ff4400']
                        });
                        return false;
                      }
                    param.topChannelName=topChannelName;
                    if($("#add_shield_type").val() == 'AREA'){
	                    var provinceName = $("#add_province option:selected").text();
	                    if ($("#add_province option:selected").val().trim().length === 0) {
	                        layer.tips('所属城市不能为空', '#add_province_tips', {
	                          tips: [2, '#ff4400']
	                        });
	                        return false;
	                      }
	                    var cityName = $("#add_city option:selected").text();
	                    param.provinceName = provinceName;
	                    param.cityName = cityName;
                    }else{
                    	if ($("#storeCodes").val().trim().length === 0) {
                    		layer.tips('门店编码不能为空', '#add_store_codes_tips', {
      	                      tips: [2, '#ff4400']
      	                    });
      	                    return false;
                    	}
                    	if (!storeCodeRegex.test($("#storeCodes").val())){
                            layer.tips('门店编码格式不正确', '#add_store_codes_tips', {
                              tips: [2, '#ff4400']
                            });
                            return false;
                        }
                    	
                    	param.provinceCode='';
                    	param.cityCode='';
                    	param.provinceName='';
                    	param.cityName='';
                    }
                    
                    if ($("#add_deliveryType option:selected").val().trim().length === 0) {
                        layer.tips('请选择屏蔽类型', '#add_deliveryType_tips', {
                        tips: [2, '#ff4400']
                        });
                        return false;
                     }
                    console.log(param);
                    $X("/logistics/logisticsShield", "updateShield").callx({
                        param: param,
                        onResult: function (data) {
                            if (data) {
                                $.openx.tableRefresh(null, null, true);
                                layer.closeAll();
                                layer.msg("更新成功");
                            }
                        },
                        onError: function (error) {
                            var res = JSON.parse(error);
                            layer.msg(res.message)
                        }
                    });

                }
            });
        });
        $.ajaxSetup({async: false});
        initAreaSelect("add_province", "add_city", "district");
        initChannelAndShield("add_channel","add_deliveryType","add_shield_type");
        //根据receiveAreaName初始化select下拉框
        if(row[0].receiveAreaName != undefined){
	        var name = row[0].receiveAreaName.split("-");
	        if(name[1] =="*"){
	            $('#add_province option').filter(function () {
	                return $(this).html() == name[0]; }).attr("selected", true);
	            $('#add_province').trigger("change");
	        }else{
	            $('#add_province option').filter(function () {
	                return $(this).html() == name[0]; }).attr("selected", true);
	            //自动触发onchange
	            $('#add_province').trigger("change");
	            $('#add_city option').filter(function () {
	                return $(this).html() == name[1]; }).attr("selected", true);
	        }
        }else{
        	$("#add_shield_type").val("STORE");
        	$("#add_shield_type").change();
        }
        
        $("#add_channel option[value='"+row[0].topChannelCode+"']").attr("selected", true);
        $("#storeCodes").val(row[0].storeCodes);
        $("#add_id").val(row[0].id);
        $("#add_deliveryType option[value='"+row[0].deliveryType+"']").attr("selected", true);
    });


};




//删除操作
$.openx.delete = function () {
    $.openx.onClickDelete(function (row) {
        $X("/logistics/logisticsShield", "deleteShield").callx({
            id: row[0].id,
            onResult: function (data) {
                $.openx.tableRefresh(null, null, true);
                layer.msg("删除成功");
            },
            onError: function (error) {
                layer.msg("系统错误，请联系管理员");
            }
        });
    });
};


$.openx.query = function () {
    if($("#province option:selected").val()){
        var provinceName = $("#province option:selected").text();
    }
    if($("#city option:selected").val()){
        var cityName = $("#city option:selected").text();
    }else{
        var cityName="";
    }
    if($("#province option:selected").val()||$("#city option:selected").val()){
        $("#receiveAreaName").val(provinceName+"-"+cityName)
    }else{
        $("#receiveAreaName").val("");
    }

    var data = $.openx.getJsonObj(queryForm);
    param.bean = data;
    $.openx.tableRefresh(url, param);

}